package com.sunlake.spring.main.model.article.controller;

import com.sunlake.spring.main.common.response.R;
import com.sunlake.spring.main.model.article.dto.CreateArticlesDTO;
import com.sunlake.spring.main.model.article.dto.FetchArticlesDTO;
import com.sunlake.spring.main.model.article.service.ArticleService;
import com.sunlake.spring.main.model.article.vo.uploadCoverVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

@RestController
@RequestMapping("/api/articles")
public class ArticleController {

    @Autowired
    private ArticleService articleService;

    @GetMapping("/list")
    public R fetchArticles(@RequestParam int page,
                           @RequestParam int size,
                           @RequestParam(required = false) String category,
                           @RequestParam(required = false) String keyword) {
        FetchArticlesDTO fetchArticlesDTO = new FetchArticlesDTO(page, size, category, keyword);
        System.out.println(fetchArticlesDTO);
        return articleService.fetchArticles(fetchArticlesDTO);
    }

    @PostMapping("/create-article-meta")
    public R createArticle(@RequestBody CreateArticlesDTO createArticlesDTO) {
        return articleService.createArticle(createArticlesDTO);
    }

    @PostMapping("/upload-cover")
    public R uploadCover(@RequestParam("file") MultipartFile file) {
        return articleService.uploadCover(file);
    }

}
